package com.jk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jk.model.CollectBean;
import com.jk.model.ProductBean;
import com.jk.model.UserBean;
import com.jk.service.UserService;
import com.jk.utils.OSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("user")
public class UserController {

    @Reference(check = false,timeout = 100000)//check = false 这个注解不加  必须跑多个生产者
    private UserService userService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    /**
     * 平台 卖家管理 整顿中的商家
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @RequestMapping("findUserShopListZhengDun")
    @ResponseBody
    public Map findUserShopListZhengDun(Integer page, Integer rows, UserBean userBean){
        return userService.findUserShopListZhengDun(page,rows,userBean);
    }
    /**
     * 平台 卖家管理 营业中的商家
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @RequestMapping("findUserShopListYingYe")
    @ResponseBody
    public Map findUserShopListYingYe(Integer page, Integer rows, UserBean userBean){
        return userService.findUserShopListYingYe(page,rows,userBean);
    }
    /**
     * 平台 卖家管理 审核失败的商家
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @RequestMapping("findUserShopListShiBai")
    @ResponseBody
    public Map findUserShopListShiBai(Integer page, Integer rows, UserBean userBean){
        return userService.findUserShopListShiBai(page,rows,userBean);
    }
    /**
     * 平台 卖家管理 冻结的商家
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @RequestMapping("findUserShopListDongJie")
    @ResponseBody
    public Map findUserShopListDongJie(Integer page, Integer rows, UserBean userBean){
        return userService.findUserShopListDongJie(page,rows,userBean);
    }

    /**
     * 审核店铺的操作
     * @param falg
     * @return
     */
    @RequestMapping("caozuo")
    @ResponseBody
    public Boolean caozuo(Integer id,Integer falg){
        try {
            userService.caozuo(id,falg);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询收藏夹中的内容 mongodb
     */
    @RequestMapping("queryCollect")
    @ResponseBody
    public Map queryCollect(HttpServletRequest request){
        System.out.println("查询收藏夹的内容");
        HashMap<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession();
        UserBean user = (UserBean) session.getAttribute(session.getId());
        if (user == null){
            result.put("code",1);
            result.put("msg","请登录");
            return result;
        }
        Integer id = user.getId();
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        List<CollectBean> collectBeans = mongoTemplate.find(query, CollectBean.class);
        ArrayList<ProductBean> list = new ArrayList<>();
        for (CollectBean collectBean : collectBeans) {
            ProductBean productBean = userService.queryProductByInfoProductId(collectBean.getProductId());
            list.add(productBean);
        }
        result.put("rows",list);
        return result;
    }

    /**
     *加入收藏夹  mongodb
     */
    @RequestMapping("saveCollect")
    @ResponseBody
    public Map saveCollect(Integer productId,HttpServletRequest request ){
        System.out.println("加入收藏夹");
        HashMap<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession();
        UserBean userBean = (UserBean) session.getAttribute(session.getId());
        if (userBean == null){
            result.put("code",1);
            result.put("msg","请先登录");
            return result;
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userBean.getId()));
        query.addCriteria(Criteria.where("productId").is(productId));
        List<CollectBean> collectBeans = mongoTemplate.find(query, CollectBean.class);
        if (collectBeans.size()!=0){
            result.put("code",2);
            result.put("msg","商品已存在收藏夹");
            return result;
        }
        CollectBean collectBean = new CollectBean();
        //用户Id存入收藏夹实体Bean
        collectBean.setUserId(userBean.getId());
        //商品Id存入收藏夹实体Bean
        collectBean.setProductId(productId);
        //保存时间存入收藏夹实体Bean
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        collectBean.setCreateTime(format);
        result.put("code",0);
        result.put("msg","加入成功");
        mongoTemplate.save(collectBean);
        return result;
    }




    /**
     * 审核商品的操作
     * @param falg
     * @return
     */
    @RequestMapping("pizhun")
    @ResponseBody
    public Boolean pizhun(Integer id,Integer falg){
        try {
            userService.pizhun(id,falg);
            if(falg==1){
                ArrayList<IndexQuery> indexQueries = new ArrayList<>();
                ProductBean productBean = userService.findProduct(id);
                IndexQuery indexQuery = new IndexQueryBuilder()
                        .withId(productBean.getId().toString())
                        .withObject(productBean)
                        .build();
                indexQueries.add(indexQuery);
                elasticsearchTemplate.bulkIndex(indexQueries);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 审核商品的查询
     * @param page
     * @param rows
     * @param userBean
     * @return
     */
    @RequestMapping("findShopProductList")
    @ResponseBody
    public Map findUserShopList(Integer page, Integer rows, UserBean userBean){
        return userService.findShopProductList(page,rows,userBean);
    }

    //图片上传
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile imgfile, HttpServletRequest request) throws IOException {

        OSSClientUtil ossClient = new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(imgfile);
        String fileUpload = ossClient.getImgUrl(name);
        HashMap<String, Object> result = new HashMap<>();
        result.put("img", fileUpload);
        return result;
    }

    @RequestMapping("get")
    @ResponseBody
    public Integer getCount(){
        return userService.getCount();
    }

    @RequestMapping("get1")
    public void get1(){

        for (int i = 0; i <10 ; i++) {
            System.out.println("测试123132");
        }

    }


}
